package com.evil.common.application.enums.judge;

import com.evil.common.application.dto.judge.Judge;
import com.evil.common.application.dto.judge.JudgeAnd;
import com.evil.common.application.dto.judge.JudgeOr;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 筛选类型（1-或条件，2-且条件）
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum JudgeTypeEnum {
    /**
     * 筛选类型（1-或条件，2-且条件）
     */
    OR(1, "或条件", JudgeOr.class),
    AND(2, "且条件", JudgeAnd.class),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final Class<? extends Judge> jClass;

    public static Optional<JudgeTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static JudgeTypeEnum findById(Integer id) {
        return JudgeTypeEnum.isExistById(id).orElseThrow(JudgeTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "自增值重置周期参数错误");
    }
}
